package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Advertisement;
import com.kun.index.entity.Inform;
import com.kun.index.mapper.AdvertisementMapper;
import com.kun.index.service.AdvertisementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-04-05
 */
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements AdvertisementService {

    @Autowired
    private AdvertisementMapper advertisementMapper;


    @Override
    public Map<String, Object> queryAdvertisements(HashMap<String, Object> map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Advertisement> wrapper = new QueryWrapper<>();

        if (map.get("queryTitleLike")!=null){
            wrapper.like("title",map.get("queryTitleLike"));
        }
        if (map.get("auditStatue")!=null){
            if ((Boolean)map.get("auditStatue")){           //查询审核成功的广告
                wrapper.eq("auditStatue",1);
            }else {             //查询审核失败的广告
                wrapper.eq("auditStatue",0);
            }
        }
        if (map.get("pageIndex")!=null&&map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Advertisement> page = new Page<>(pageIndex,pageSize);
            advertisementMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Advertisement> advertisements = page.getRecords();
            resultMap.put("totalAdvertisementsNum",total);
            resultMap.put("advertisements",advertisements);
        }else {
            List<Advertisement> advertisements = advertisementMapper.selectList(wrapper);
            resultMap.put("advertisements",advertisements);
        }
        return resultMap;
    }

    @Override
    public Integer addAdvertisement(Advertisement advertisement) {
        advertisementMapper.insert(advertisement);
        return advertisement.getId();
    }

    @Override
    public Advertisement queryAdvertisementById(Integer advertisementId) {

        return advertisementMapper.selectById(advertisementId);
    }

    @Override
    public Integer updateAdvertisement(Advertisement advertisement) {
        return advertisementMapper.updateById(advertisement);
    }

    @Override
    public int deleteAdvertisement(Advertisement advertisement) {
        return advertisementMapper.deleteById(advertisement);
    }

    @Override
    public int batchDeleteInform(List<Integer> ids) {
        return advertisementMapper.deleteBatchIds(ids);
    }
}
